
package net.swa.dewei.web.action;

import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Required;
import net.swa.dewei.beans.entity.LifeGoods;
import net.swa.dewei.service.LifeGoodsService;
import net.swa.system.service.ICommonService;
import net.swa.system.util.annotation.Log;
import net.swa.system.web.action.AbstractBaseAction;
import net.swa.util.JsonResult;

/**一卡通商城信息表 Action**/
public class LifeGoodsAction extends AbstractBaseAction
{
    private static final long serialVersionUID = -9167064623097370397L;

    private final Logger log = Logger.getLogger(LifeGoodsAction.class);

    /** 公用方法接口 */
    private ICommonService commonService;

    private LifeGoodsService lifeGoodService;

    //实体类id
    private Long id;

    //名称
    private String name;

    //品牌
    private String pinpai;

    //规格
    private String guige;

    //多图片地址
    private String[] imgs;

    //广告实体类
    private LifeGoods model;

    //id集合
    private Long[] ids;

    private Integer status;

    /**初始化分页查询**/
    @Log(description = "进入一卡通商城分页查询")
    public String listPage()
    {
        return "listPage";
    }

    public String queryPage() throws Exception
    {
        currentPage = currentPage == 0 ? 1 : currentPage;
        pageSize = pageSize == 0 ? 20 : pageSize;
        JsonResult<LifeGoods> json = null;
        //支持多种条件查询分页便于扩展
        json = lifeGoodService.queryPage(name, pinpai, guige, currentPage, pageSize, orderBy, orderType);
        json.setCurrentPage(currentPage);
        outJson(json);
        return null;
    }

    @Log(description = "去新增或者修改一卡通商品页面")
    public String toEditLifeGoods() throws Exception
    {
        log.debug("编辑广告信息");
        if (null != id)
        {
            model = lifeGoodService.queryById(id);
            if (null != model.getImageUrl())
            {
                imgs = model.getImageUrl().split(",");
            }
        }
        else
        {
            model = new LifeGoods();
        }
        return "toEditLifeGoods";
    }

    @Log(description = "新增或者修改得威一卡通商品")
    public String saveLifeGoods() throws Exception
    {
        boolean flag;
        process(imgs);
        if (null == model.getId())
        {
            log.debug("新增：" + model);
            flag = lifeGoodService.add(model);
        }
        else
        {
            log.debug("修改：" + model);
            flag = lifeGoodService.update(model);
        }
        if (flag)
        {
            outSuccess();
        }
        else
        {
            outError("error");
        }
        return null;
    }

    private void process(String[] images)
    {
        if (null != images && images.length > 0)
        {
            String imgs = "";
            for (String s : images)
            {
                if (!StringUtils.isBlank(s))
                {
                    imgs += s + ",";
                }
            }
            model.setImageUrl(imgs);
        }

    }

    @Log(description = "得威一卡通商品批量下架")
    public String batchUpdateXiajia() throws Exception
    {
        Map<String, Object> map = null;
        map = lifeGoodService.batchUpdateXiajia(ids);
        if (null != map)
        {
            outJson(map);
        }
        else
        {
            outError("ERROR");
        }
        return null;
    }

    @Log(description = "得威一卡通商品发布下架")
    public String updateState() throws Exception
    {
        Map<String, Object> map = null;
        map = lifeGoodService.updateState(id, status);
        if (null != map)
        {
            outJson(map);
        }
        else
        {
            outError("ERROR");
        }
        return null;

    }

    @Required
    @Resource
    public void setCommonService(ICommonService commonService)
    {
        this.commonService = commonService;
    }

    @Required
    @Resource
    public void setLifeGoodsService(LifeGoodsService lifeGoodService)
    {
        this.lifeGoodService = lifeGoodService;
    }

    public Long getId()
    {

        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public Long[] getIds()
    {
        return ids;
    }

    public void setIds(Long[] ids)
    {
        this.ids = ids;
    }

    public ICommonService getCommonService()
    {
        return commonService;
    }

    public String[] getImgs()
    {
        return imgs;
    }

    public void setImgs(String[] imgs)
    {
        this.imgs = imgs;
    }

    public LifeGoods getModel()
    {
        return model;
    }

    public void setModel(LifeGoods model)
    {
        this.model = model;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getPinpai()
    {
        return pinpai;
    }

    public void setPinpai(String pinpai)
    {
        this.pinpai = pinpai;
    }

    public String getGuige()
    {
        return guige;
    }

    public void setGuige(String guige)
    {
        this.guige = guige;
    }

    public Integer getStatus()
    {
        return status;
    }

    public void setStatus(Integer status)
    {
        this.status = status;
    }

}
